package com.mage.servlet;

import com.mage.bean.User;
import com.mage.dao.ChongzhiDao;
import com.mage.dao.UserDao;
import com.mage.dao.impl.ChongzhiDaoImpl;
import com.mage.dao.impl.UserDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/VipTuifeiServlet")
public class VipTuifeiServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public VipTuifeiServlet() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            //1. 接收id
            int id = Integer.parseInt(request.getParameter("id"));
            int userid = Integer.parseInt(request.getParameter("userid"));
            double money = Double.parseDouble(request.getParameter("money"));
            System.out.println("deleteing .....");
            //2. 执行删除
            ChongzhiDao service = new ChongzhiDaoImpl();
            service.delete(id);
            UserDao userservice = new UserDaoImpl();
            User user = userservice.findUserById(userid);
            if (user.getWxf() < money){
                money = user.getWxf();
            }
            user.setWxf(user.getWxf()-money);
            userservice.update(user);
            System.out.println("deleted .....");
            //3. 跳转到列表页。
            request.getRequestDispatcher("/Super/User_TuifeiAlert.jsp").forward(request, response);
        }catch (Exception e) {
        }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
